/* Copyright (c) 2009, Antonie Jovanoski
 *	
 * All rights reserved.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 * Contact e-mail: Antonie Jovanoski <minimoog77@gmail.com>
 */

#include "qtwitdget.h"
#include "userpassdialog.h"

QTwitdget::QTwitdget(QWidget *parent)
	:	QWidget(parent), 
		m_friendsTimeline(new QTwitFriendsTimeline(this)),
		m_twitUpdate(new QTwitUpdate(this)),
		m_imageDownloader(new ImageDownloader(this))
{
	ui.setupUi(this);
	connect(ui.userpassButton, SIGNAL(clicked()), SLOT(userpassButtonClicked()));
	connect(ui.updateButton, SIGNAL(clicked()), SLOT(updateButtonClicked()));
	connect(m_friendsTimeline, SIGNAL(finished()), SLOT(finishedFriendsTimeline()));
	connect(m_imageDownloader, SIGNAL(finished()), SLOT(finishedDownloadImages()));
	connect(ui.updateEdit, SIGNAL(overLimit(bool)), ui.updateButton, SLOT(setDisabled(bool)));
	connect(ui.updateEdit, SIGNAL(returnPressed()), ui.updateButton, SLOT(click()));

	ui.updateEdit->setLimit(140);

	//read last username
	QSettings settings("QTwitdget", "QTwitdget");
	m_username = settings.value("LastUsername").toString();

	userpassButtonClicked();
}

void QTwitdget::userpassButtonClicked()
{
	UserPassDialog upd(this);
	upd.setUsername(m_username);
	if(!m_username.isEmpty())
		upd.setPasswordEditFocut();

	//TODO: Use verify_credentials Twitter api to validate username and password

	QTimer *timer;

	if(QDialog::Accepted == upd.exec()){
		setUserPass(upd.username(), upd.password());

		//store last used username
		QSettings settings("QTwitdget", "QTwitdget");
		settings.setValue("LastUsername", upd.username());

		timer = new QTimer(this);
		connect(timer, SIGNAL(timeout()), SLOT(updateTimeline()));
		timer->start(60000);

		updateTimeline();
	}
}

void QTwitdget::updateTimeline()
{
	m_friendsTimeline->update(QDateTime(), 0, 5, 0);
}

void QTwitdget::setUserPass(const QString &username, const QString &password)
{
	m_friendsTimeline->setUsername(username);
	m_friendsTimeline->setPassword(password);
	m_twitUpdate->setUsername(username);
	m_twitUpdate->setPassword(password);
	m_username = username;
	m_password = password;
}

void QTwitdget::updateButtonClicked()
{
	if(!ui.updateEdit->toPlainText().isEmpty()){
		//trim to 140 characters
		QString updateText = ui.updateEdit->toPlainText().left(140);
		m_twitUpdate->setUpdate(updateText);
		ui.updateEdit->clear();
	}
}

void QTwitdget::finishedFriendsTimeline()
{
	QList<QTwitStatus> statuses = m_friendsTimeline->getStatuses();

	//build url image list for downloading
	QList<QUrl> urlsImages;
	foreach(QTwitStatus s, statuses)
		urlsImages << s.profileImageUrl();

	//download/load images
	m_imageDownloader->downloadImages(urlsImages);

	//just testing
	ui.statusWidget0->setText(statuses.at(0).text());
	ui.statusWidget0->setScreenName(statuses.at(0).screenName());
	ui.statusWidget0->setCreated(statuses.at(0).created());

	ui.statusWidget1->setText(statuses.at(1).text());
	ui.statusWidget1->setScreenName(statuses.at(1).screenName());
	ui.statusWidget1->setCreated(statuses.at(1).created());

	ui.statusWidget2->setText(statuses.at(2).text());
	ui.statusWidget2->setScreenName(statuses.at(2).screenName());
	ui.statusWidget2->setCreated(statuses.at(2).created());

	ui.statusWidget3->setText(statuses.at(3).text());
	ui.statusWidget3->setScreenName(statuses.at(3).screenName());
	ui.statusWidget3->setCreated(statuses.at(3).created());

	ui.statusWidget4->setText(statuses.at(4).text());
	ui.statusWidget4->setScreenName(statuses.at(4).screenName());
	ui.statusWidget4->setCreated(statuses.at(4).created());
}

void QTwitdget::finishedDownloadImages()
{
	QHash<QString, QImage> images = m_imageDownloader->getImages();
	QList<QTwitStatus> statuses = m_friendsTimeline->getStatuses();

	//just testing
	QImage img0 = images.value(statuses.at(0).profileImageUrl());
	QImage img1 = images.value(statuses.at(1).profileImageUrl());
	QImage img2 = images.value(statuses.at(2).profileImageUrl());
	QImage img3 = images.value(statuses.at(3).profileImageUrl());
	QImage img4 = images.value(statuses.at(4).profileImageUrl());

	ui.statusWidget0->setProfileImagePixmap(QPixmap::fromImage(img0));
	ui.statusWidget1->setProfileImagePixmap(QPixmap::fromImage(img1));
	ui.statusWidget2->setProfileImagePixmap(QPixmap::fromImage(img2));
	ui.statusWidget3->setProfileImagePixmap(QPixmap::fromImage(img3));
	ui.statusWidget4->setProfileImagePixmap(QPixmap::fromImage(img4));
}
	